// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.util.io;

/**
 * Barely, a copy of java.io.Bits, which is for unknown reason package-private.
 */
@SuppressWarnings({"PointlessBitwiseExpression", "PointlessArithmeticExpression"})
public final class Bits {

  /*
   * Methods for unpacking primitive values from byte arrays starting at
   * given offsets.
   */

  public static boolean getBoolean(byte[] b, int off) {
    return b[off] != 0;
  }

  public static char getChar(byte[] b, int off) {
    return (char)(((b[off + 1] & 0xFF) << 0) +
                  ((b[off + 0] & 0xFF) << 8));
  }

  public static short getShort(byte[] b, int off) {
    return (short)(((b[off + 1] & 0xFF) << 0) +
                   ((b[off + 0] & 0xFF) << 8));
  }

  public static int getInt(byte[] b, int off) {
    return ((b[off + 3] & 0xFF) << 0) +
           ((b[off + 2] & 0xFF) << 8) +
           ((b[off + 1] & 0xFF) << 16) +
           ((b[off + 0] & 0xFF) << 24);
  }

  public static float getFloat(byte[] b, int off) {
    int i = ((b[off + 3] & 0xFF) << 0) +
            ((b[off + 2] & 0xFF) << 8) +
            ((b[off + 1] & 0xFF) << 16) +
            ((b[off + 0] & 0xFF) << 24);
    return Float.intBitsToFloat(i);
  }

  public static long getLong(byte[] b, int off) {
    return ((b[off + 7] & 0xFFL) << 0) +
           ((b[off + 6] & 0xFFL) << 8) +
           ((b[off + 5] & 0xFFL) << 16) +
           ((b[off + 4] & 0xFFL) << 24) +
           ((b[off + 3] & 0xFFL) << 32) +
           ((b[off + 2] & 0xFFL) << 40) +
           ((b[off + 1] & 0xFFL) << 48) +
           ((b[off + 0] & 0xFFL) << 56);
  }

  public static double getDouble(byte[] b, int off) {
    long j = ((b[off + 7] & 0xFFL) << 0) +
             ((b[off + 6] & 0xFFL) << 8) +
             ((b[off + 5] & 0xFFL) << 16) +
             ((b[off + 4] & 0xFFL) << 24) +
             ((b[off + 3] & 0xFFL) << 32) +
             ((b[off + 2] & 0xFFL) << 40) +
             ((b[off + 1] & 0xFFL) << 48) +
             ((b[off + 0] & 0xFFL) << 56);
    return Double.longBitsToDouble(j);
  }

  /*
   * Methods for packing primitive values into byte arrays starting at given
   * offsets.
   */

  public static void putBoolean(byte[] b, int off, boolean val) {
    b[off] = (byte)(val ? 1 : 0);
  }

  public static void putChar(byte[] b, int off, char val) {
    b[off + 1] = (byte)(val >>> 0);
    b[off + 0] = (byte)(val >>> 8);
  }

  public static void putShort(byte[] b, int off, short val) {
    b[off + 1] = (byte)(val >>> 0);
    b[off + 0] = (byte)(val >>> 8);
  }

  public static void putInt(byte[] b, int off, int val) {
    b[off + 3] = (byte)(val >>> 0);
    b[off + 2] = (byte)(val >>> 8);
    b[off + 1] = (byte)(val >>> 16);
    b[off + 0] = (byte)(val >>> 24);
  }

  public static void putFloat(byte[] b, int off, float val) {
    int i = Float.floatToIntBits(val);
    putInt(b, off, i);
  }

  public static void putLong(byte[] b, int off, long val) {
    b[off + 7] = (byte)(val >>> 0);
    b[off + 6] = (byte)(val >>> 8);
    b[off + 5] = (byte)(val >>> 16);
    b[off + 4] = (byte)(val >>> 24);
    b[off + 3] = (byte)(val >>> 32);
    b[off + 2] = (byte)(val >>> 40);
    b[off + 1] = (byte)(val >>> 48);
    b[off + 0] = (byte)(val >>> 56);
  }

  public static void putDouble(byte[] b, int off, double val) {
    long j = Double.doubleToLongBits(val);
    putLong(b, off, j);
  }
}
